//实现成绩录入
//有两个功能1：成绩查询 2：点击数据编辑

let tbody = document.querySelector('tbody');

getscore()
//实现成绩查询
function getscore() {
    axios.get('/score/list').then(res => {
        if (res.data.code === 0) {
            //数据渲染
            let obj = res.data.data
            let HTML = ''
            for (const key in obj) {
                
                let a = obj[key].score.reduce((h, v, i) => h + `
            <td class="score">${v}<input data-batch=${i+1} data-id=${key} type="text"></td>
            
            `, ``)
                HTML += `
                <tr>
        <th scope="row">${key}</th>
        <td>${obj[key].name}</td>
         ${a}   
        </tr>
                `
            }
            tbody.innerHTML = HTML
        }
        
    })
}



//实现点击数据编辑数据

tbody.addEventListener('dblclick',(e)=>{
    const dom = e.target
    if(e.target.nodeName === 'TD'){
       dom.querySelector('input').style.display = 'block'
        dom.querySelector('input').focus()
        dom.querySelector('input').value = e.target.innerText
        dom.querySelector('input').addEventListener('blur',()=>{
            let data = {
                stu_id: dom.querySelector('input').dataset.id,
                batch: dom.querySelector('input').dataset.batch,
                score: dom.querySelector('input').value
            }
            axios.post('/score/entry', data).then(res => {
                if (res.data.code === 0) {
                    toastr.success(res.data.message)
                    getscore()
                }
            })
        })
    }
    
})